﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace XMM.Dialoges
{
    /// <summary>
    /// Interaktionslogik für ManageWatchfolders.xaml
    /// </summary>
    public partial class ManageWatchfolders : Window
    {
        ObservableCollection<Objects.Watchfolder> _Watchfolders = new ObservableCollection<Objects.Watchfolder>();
        public ObservableCollection<Objects.Watchfolder> Watchfolders { get { return _Watchfolders; } }

        private XMM.Library ParentLibrary;

        public ManageWatchfolders(XMM.Library dlgParentLibrary)
        {
            InitializeComponent();

            ParentLibrary = dlgParentLibrary;

            loadWatchfolders();
        }

        private void loadWatchfolders()
        {
            _Watchfolders = new ObservableCollection<Objects.Watchfolder>();
            List<Objects.Watchfolder> lWatchfolders = Objects.Watchfolder.getAllWatchfolders();

            if (lWatchfolders != null && lWatchfolders.Count > 0)
            {
                foreach (Objects.Watchfolder oWatchfolder in lWatchfolders)
                {
                    _Watchfolders.Add(oWatchfolder);
                }
            }

            lvWatchfolders.ItemsSource = Watchfolders;
            lvWatchfolders.Items.Refresh();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            ParentLibrary.RefreshWatchfolders();
            Close();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dlgBrowseFolder = new System.Windows.Forms.FolderBrowserDialog();
            dlgBrowseFolder.ShowNewFolderButton = true;

            if (dlgBrowseFolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Objects.Watchfolder oWatchfolder = new Objects.Watchfolder(dlgBrowseFolder.SelectedPath);
                if (oWatchfolder.SaveNew())
                    loadWatchfolders();
            }
        }

        private void mitDelete_Click(object sender, RoutedEventArgs e)
        {
            if (lvWatchfolders.SelectedIndex > -1)
            {
                Objects.Watchfolder oWatchfolder = (Objects.Watchfolder)lvWatchfolders.SelectedItem;

                if (MessageBox.Show("Möchten Sie die Verknüpfung mit \"" + oWatchfolder.Path + "\" wirklich löschen?", "Verknüpfung löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    if (oWatchfolder.Delete())
                    {
                        loadWatchfolders();
                    }
                }
            }
            else
            {
                MessageBox.Show("Bitte wählen Sie zuerst eine Verknüpfung aus der Liste!", "Kein Verknüpfung gewählt", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
    }
}
